import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

/**
 * User: rmikhaylenko mirusnet@gmail.com
 * Date: 08/08/13
 * Time: 16:58
 */

public class ButtonAction implements ActionListener {
    public static boolean quit = false;

    @Override
    public void actionPerformed(ActionEvent e) {
        int fileCount = PlaylistUtils.getCountOfFilesInPlaylist(e.getActionCommand());
        int startPosition = PlaylistUtils.getStartPosition(e.getActionCommand());

        ArrayList<String> list = PlaylistUtils.getPlaylist(e.getActionCommand(), startPosition);

        //PlayInThread p = new PlayInThread(MplayerLine.getExecLine(e.getActionCommand(), list.get(0)));
        //p.start();

        Process p;
        for (String filePath : list) {
            if(quit) {
                quit = false;
                break;
            }

            try {
                p = Runtime.getRuntime().exec(MplayerLine.getExecLine(e.getActionCommand(), filePath));

                ReadInThread readInThread = new ReadInThread(p, e.getActionCommand(), startPosition, fileCount);
                readInThread.start();

                while (readInThread.isAlive()) {
                    Mplayer.askTime(p);
                    Thread.sleep(5000);
                }
                ++startPosition;

            } catch (IOException | InterruptedException e1) {
                e1.printStackTrace();
            }

        }

    }
}
